//
//  BCIndustryAdsCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/21.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCIndustryAdsCell.h"

//
#import "UICollectionViewLeftAlignedLayout.h"
#import "BCIndustryAdsCollectionCell.h"

#import "BCIndustrySectionModel.h"

static CGFloat BCIndustryAdsCellLeftPadding = 10.0;
static CGFloat BCIndustryAdsCellTopPadding = 20.0f;
static CGFloat BCIndustryAdsCellPadding = 10.0f;

@interface BCIndustryAdsCell () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *adCollection;

/**
 *ads
 */
@property (nonatomic, copy) NSArray *ads;

@end

@implementation BCIndustryAdsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    UICollectionViewLeftAlignedLayout *layout = [UICollectionViewLeftAlignedLayout new];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(BCIndustryAdsCellTopPadding*WidthScale, BCIndustryAdsCellLeftPadding*WidthScale, BCIndustryAdsCellTopPadding*WidthScale, BCIndustryAdsCellLeftPadding*WidthScale);
    layout.minimumLineSpacing = BCIndustryAdsCellPadding*WidthScale;
    layout.minimumInteritemSpacing = BCIndustryAdsCellPadding*WidthScale;
    self.adCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.contentView addSubview:self.adCollection];
    [self.adCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    self.adCollection.backgroundColor = [UIColor clearColor];
    self.adCollection.dataSource = self;
    self.adCollection.delegate = self;
    self.adCollection.showsHorizontalScrollIndicator = false;
    [self.adCollection registerClass:BCIndustryAdsCollectionCell.class forCellWithReuseIdentifier:NSStringFromClass(BCIndustryAdsCollectionCell.class)];
}

- (void)setSectionModel:(BCIndustrySectionModel *)sectionModel {
    _sectionModel = sectionModel;
    //
    self.ads = sectionModel.ads;
    [self.adCollection reloadData];
}

#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.ads.count;;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCIndustryAdsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(BCIndustryAdsCollectionCell.class) forIndexPath:indexPath];
    cell.adModel = self.ads[indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = collectionView.frame.size.width;
    return [self.class itemSizeForWidth:width];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BCIndustryDataTitleGroupAdModel *model = self.ads[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(industryAdsCell:didSelectedAdModel:)]) {
        [self.delegate industryAdsCell:self didSelectedAdModel:model];
    }
}

+ (CGSize)itemSizeForWidth:(CGFloat)width {
    //
    CGFloat leftPadding = BCIndustryAdsCellLeftPadding*WidthScale;
    CGFloat padding = BCIndustryAdsCellPadding*WidthScale;
    //
    CGFloat cellWidth = (width-2*leftPadding-padding)/2.0;
    CGFloat cellHeight = cellWidth*(96.0/173.0);
    return CGSizeMake(cellWidth, cellHeight);
}

+ (CGFloat)heightWithWidth:(CGFloat)width {
    CGFloat topPadding = BCIndustryAdsCellTopPadding*WidthScale;
    CGSize cellSize = [self itemSizeForWidth:width];
    CGFloat height = topPadding*2+cellSize.height;
    return height;
}

@end
